using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

namespace Departure
{
    public class ctrlStationItem : KListControl.IKListItem, IDisposable
    {

        #region Delegates

        public delegate void CountdownEventDelegate(string time, string train, string direction, string platform, string later, string fullDateTime);

        #endregion Delegates

        #region Events

        public event CountdownEventDelegate OnCountdown;

        #endregion Events

        #region Variables

        string time = string.Empty;
        string train = string.Empty;
        string icon = string.Empty;
        string direction = string.Empty;
        string platform = string.Empty;
        string later = string.Empty;
        string fullDateTime = string.Empty;

        string key = string.Empty;

        private StringFormat m_stringFormat = new StringFormat();
        private KListControl m_parent;
        private Rectangle m_bounds;
        private int m_x = -1;
        private int m_y = -1;

        private string m_text;
        private object m_value;
        private bool m_selected = false;
        private string fastAccessValue;

        string timeLater = "";

        Font fontTime;
        Font fontTrain;

        private bool vga = false;
       
        #endregion Variables

        #region Constructor

        public ctrlStationItem()
        {
            m_stringFormat.Alignment = StringAlignment.Near;
            m_stringFormat.FormatFlags = StringFormatFlags.NoWrap;
        }

        public ctrlStationItem(string time, string train, string icon, string direction, string platform, string later, string fullDateTime, bool vga)
        {
            this.time = time;
            this.train = train;
            this.icon = icon;
            this.direction = direction;
            this.platform = platform;
            this.later = later;
            this.fullDateTime = fullDateTime;
            this.vga = vga;
            
            m_stringFormat.Alignment = StringAlignment.Near;
            m_stringFormat.FormatFlags = StringFormatFlags.NoWrap;

        }

        /// <summary>
        /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
        /// </summary>
        public virtual void Dispose()
        {
            m_parent = null;
        }

        #endregion Constructor

        #region Public Functions

        public void SetFontStyle()
        {
            fontTime = new Font(m_parent.Font.Name, m_parent.Font.Size, m_parent.SkinSettings.BoldTime == true ? FontStyle.Bold : FontStyle.Regular);
            fontTrain = new Font(m_parent.Font.Name, m_parent.Font.Size, m_parent.SkinSettings.BoldTrain == true ? FontStyle.Bold : FontStyle.Regular);
        }

        public void Render(Graphics g, Rectangle bounds)
        {
            if (m_parent != null)
            {
                if (vga)
                {
                    if (m_selected)
                        g.DrawImage(m_parent.SkinSettings.BackGroundItemSelected, bounds, new Rectangle(0, 0, m_parent.SkinSettings.BackGroundItemSelected.Width, m_parent.SkinSettings.BackGroundItemSelected.Height), GraphicsUnit.Pixel);
                    else
                        g.DrawImage(m_parent.SkinSettings.BackGroundItem, bounds, new Rectangle(0, 0, m_parent.SkinSettings.BackGroundItem.Width, m_parent.SkinSettings.BackGroundItem.Height), GraphicsUnit.Pixel);

                    if (later == "")
                        timeLater = time;
                    else
                        timeLater = time + " - " + later;

                    if (!string.IsNullOrEmpty(icon))
                    {
                        try
                        {
                            g.DrawImage(m_parent.TrainIcons[icon], bounds.X + 4, bounds.Y + bounds.Height / 2 - 24, new Rectangle(0, 0, 48, 48), GraphicsUnit.Pixel);
                        }
                        catch
                        {
                        }
                    }

                    if (later == "")
                        g.DrawString(timeLater, fontTime, new SolidBrush(m_parent.SkinSettings.FontColor), bounds.X + 56, bounds.Y);
                    else
                        g.DrawString(timeLater, fontTime, new SolidBrush(m_parent.SkinSettings.LaterColor), bounds.X + 56, bounds.Y);

                    using (SolidBrush writeBrush = new SolidBrush(m_parent.SkinSettings.FontColor))
                    {
                        Rectangle textBounds = new Rectangle(bounds.X + 56, bounds.Y + 28, bounds.Width - 64, bounds.Height - 18);
                        g.DrawString(train + " - " + direction + "\r\n" + platform, fontTrain, writeBrush, textBounds, m_stringFormat);
                    }
                }
                else
                {
                    if (m_selected)
                        g.DrawImage(m_parent.SkinSettings.BackGroundItemSelected, bounds, new Rectangle(0, 0, m_parent.SkinSettings.BackGroundItemSelected.Width, m_parent.SkinSettings.BackGroundItemSelected.Height), GraphicsUnit.Pixel);
                    else
                        g.DrawImage(m_parent.SkinSettings.BackGroundItem, bounds, new Rectangle(0, 0, m_parent.SkinSettings.BackGroundItem.Width, m_parent.SkinSettings.BackGroundItem.Height), GraphicsUnit.Pixel);

                    if (later == "")
                        timeLater = time;
                    else
                        timeLater = time + " - " + later;

                    if (!string.IsNullOrEmpty(icon))
                    {
                        try
                        {
                            g.DrawImage(m_parent.TrainIcons[icon], bounds.X + 2, bounds.Y + bounds.Height / 2 - 12, new Rectangle(0, 0, 24, 24), GraphicsUnit.Pixel);
                        }
                        catch
                        {
                        }
                    }

                    if (later == "")
                        g.DrawString(timeLater, fontTime, new SolidBrush(m_parent.SkinSettings.FontColor), bounds.X + 28, bounds.Y);
                    else
                        g.DrawString(timeLater, fontTime, new SolidBrush(m_parent.SkinSettings.LaterColor), bounds.X + 28, bounds.Y);

                    using (SolidBrush writeBrush = new SolidBrush(m_parent.SkinSettings.FontColor))
                    {
                        Rectangle textBounds = new Rectangle(bounds.X + 28, bounds.Y + 14, bounds.Width - 32, bounds.Height - 18);
                        g.DrawString(train + " - " + direction + "\r\n" + platform, fontTrain, writeBrush, textBounds, m_stringFormat);
                    }
                }
            }
        }

        # region Render Old
        //public void Render(Graphics g, Rectangle bounds)
        //{

        //    if (m_parent != null)
        //    {
        //        if (vga)
        //        {
        //            if (m_selected)
        //                g.DrawImage(m_parent.SkinSettings.BackGroundItemSelected, bounds, new Rectangle(0, 0, m_parent.SkinSettings.BackGroundItemSelected.Width, m_parent.SkinSettings.BackGroundItemSelected.Height), GraphicsUnit.Pixel);
        //            else
        //                g.DrawImage(m_parent.SkinSettings.BackGroundItem, bounds, new Rectangle(0, 0, m_parent.SkinSettings.BackGroundItem.Width, m_parent.SkinSettings.BackGroundItem.Height), GraphicsUnit.Pixel);

        //            if (later == "")
        //                timeLater = time;
        //            else
        //                timeLater = time + " - " + later;

        //            if (later == "")
        //                g.DrawString(timeLater, fontTime, new SolidBrush(m_parent.SkinSettings.FontColor), bounds.X + 4, bounds.Y);
        //            else
        //                g.DrawString(timeLater, fontTime, new SolidBrush(m_parent.SkinSettings.LaterColor), bounds.X + 4, bounds.Y);

        //            using (SolidBrush writeBrush = new SolidBrush(m_parent.SkinSettings.FontColor))
        //            {
        //                Rectangle textBounds = new Rectangle(bounds.X + 8, bounds.Y + 28, bounds.Width - 16, bounds.Height - 18);
        //                g.DrawString(train + " - " + direction + "\r\n" + platform, fontTrain, writeBrush, textBounds, m_stringFormat);
        //            }
        //        }
        //        else
        //        {
        //            if (m_selected)
        //                g.DrawImage(m_parent.SkinSettings.BackGroundItemSelected, bounds, new Rectangle(0, 0, m_parent.SkinSettings.BackGroundItemSelected.Width, m_parent.SkinSettings.BackGroundItemSelected.Height), GraphicsUnit.Pixel);
        //            else
        //                g.DrawImage(m_parent.SkinSettings.BackGroundItem, bounds, new Rectangle(0, 0, m_parent.SkinSettings.BackGroundItem.Width, m_parent.SkinSettings.BackGroundItem.Height), GraphicsUnit.Pixel);

        //            if (later == "")
        //                timeLater = time;
        //            else
        //                timeLater = time + " - " + later;

        //            if (later == "")
        //                g.DrawString(timeLater, fontTime, new SolidBrush(m_parent.SkinSettings.FontColor), bounds.X + 4, bounds.Y);
        //            else
        //                g.DrawString(timeLater, fontTime, new SolidBrush(m_parent.SkinSettings.LaterColor), bounds.X + 4, bounds.Y);

        //            using (SolidBrush writeBrush = new SolidBrush(m_parent.SkinSettings.FontColor))
        //            {
        //                Rectangle textBounds = new Rectangle(bounds.X + 4, bounds.Y + 14, bounds.Width - 8, bounds.Height - 18);
        //                g.DrawString(train + " - " + direction + "\r\n" + platform, fontTrain, writeBrush, textBounds, m_stringFormat);
        //            }
        //        }
        //    }
        //}
        #endregion

        #endregion Public Functions

        #region Private Functions

        private void mniCountdown_Click(object sender, EventArgs e)
        {
            if (OnCountdown != null)
                OnCountdown(time, train, direction, platform, later, fullDateTime);
        }

        #endregion Private Functions

        #region Properties

        public bool VGA
        {
            get { return this.vga; }
            set
            {
                this.vga = value;

            }
        }

        public string Time
        {
            get { return this.time; }
            set
            {
                this.time = value;

            }
        }

        public string Train
        {
            get { return this.train; }
            set
            {
                this.train = value;

            }
        }

        public string Icon
        {
            get { return this.icon; }
            set
            {
                this.icon = value;

            }
        }

        public string Direction
        {
            get { return this.direction; }
            set
            {
                this.direction = value;

            }
        }

        public string Platform
        {
            get { return this.platform; }
            set
            {
                this.platform = value;

            }
        }

        public string Later
        {
            get { return this.later; }
            set
            {
                this.later = value;

            }
        }

        public string FullDateTime
        {
            get { return this.fullDateTime; }
            set
            {
                this.fullDateTime = value;

            }
        }

        public string Key
        {
            get
            {
                return this.key;
            }
            set
            {
                this.key = value;
            }
        }

        public KListControl Parent { get { return m_parent; } set { m_parent = value; } }

        public int XIndex { get { return m_x; } set { m_x = value; } }

        public int YIndex { get { return m_y; } set { m_y = value; } }

        public bool Selected { get { return m_selected; } set { m_selected = value; } }

        public object Value { get { return m_value; } set { m_value = value; } }

        public Rectangle Bounds { get { return m_bounds; } set { m_bounds = value; } }

        public string Text { get { return m_text; } set { m_text = value; } }

        public string FastAccessValue
        {
            get { return fastAccessValue; }
            set { fastAccessValue = value; }
        }

        #endregion Properties

    }
}
